package com.gxuwz.medical.web.servlet.role;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.gxuwz.medical.dao.RoleDao;
import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.role.Role;
import com.gxuwz.medical.tools.JsonUtil;
import com.gxuwz.medical.tools.PageBean;
import com.gxuwz.medical.tools.ResponseUtil;
import com.gxuwz.medical.web.servlet.BaseServlet;
import com.gxuwz.medical.web.servlet.LoginServlet;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @ClassName: RoleListServlet
 * @Description: 权限信息查询
 * @author 陈震洺
 * @date 2019年3月15日 上午10:52:07
 *
 */
public class RoleListServlet extends BaseServlet {
	RoleDao roleDao = new RoleDao(); // 用户数据操作类
	DbUtil dbUtil = new DbUtil();
	private static final Logger logger = LogManager.getLogger(LoginServlet.class);

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("进入角色查询");
		list(request, response);

	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		// 查询是获得前台传来的gradeName
		String roleName = request.getParameter("roleName");
		if (roleName == null) {
			roleName = ""; // 这里置为空串，GradeDao用isEmpty判断
		}
		Role role = new Role();
		role.setRoleName(roleName);
		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Connection con = null;
		try {
			con = dbUtil.getCon();
			JSONObject result = new JSONObject();
			JSONArray jsonArray = JsonUtil.formatRsToJsoarray(roleDao.roleList(con, pageBean, role));
			System.out.println(jsonArray);
			int total = roleDao.roleCount(con, role);
			result.put("rows", jsonArray);
			result.put("total", total);
			ResponseUtil.write(response, result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
